package com.thunbu.thunbu_meeting_system.activity

import android.app.Activity
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.cathu.lib_commonkt.base.BaseMVVMActivity
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.XPopup
import com.thunbu.lib_common.widget.CommonToolBar
import com.thunbu.thunbu_meeting_system.BaseVBQuickAdapter
import com.thunbu.thunbu_meeting_system.R
import com.thunbu.thunbu_meeting_system.bean.DepartmentModel
import com.thunbu.thunbu_meeting_system.bindAdapter
import com.thunbu.thunbu_meeting_system.databinding.ActivityInventoryDetailBinding
import com.thunbu.thunbu_meeting_system.databinding.InventoryTaskListItemBinding
import com.thunbu.thunbu_meeting_system.databinding.ItemInventoryDetailBinding
import com.thunbu.thunbu_meeting_system.ternary
import com.thunbu.thunbu_meeting_system.utils.TreeViewUtils
import com.thunbu.thunbu_meeting_system.view.treeView.NodeModel
import com.thunbu.thunbu_meeting_system.view.treeView.TreeNode
import com.thunbu.thunbu_meeting_system.view.treeView.TreeViewPopupView
import com.thunbu.thunbu_meeting_system.viewMdel.InventoryDetailVM

/**
 * @Description: 盘点卡片详情
 * @author: Yang.Yang33
 * @Date: 2023/3/20 20:41
 */
class InventoryDetailActivity : BaseMVVMActivity<ActivityInventoryDetailBinding, InventoryDetailVM>() {
    companion object{
        const val ID = "ID"
        const val FROM_ASSETS = "FROM_ASSETS"
        const val IS_EDIT = "IS_EDIT"
    }
    lateinit var toolBar: CommonToolBar
    private lateinit var mAdapter: BaseVBQuickAdapter<Pair<String,String?>, ItemInventoryDetailBinding>
    var userNodes:MutableList<TreeNode<*>> = mutableListOf()
    var userNodeid:Long? = null
    var departmentNodes:MutableList<TreeNode<*>> = mutableListOf()
    var departmentNodeid:Long? = null
    var addressNodes:MutableList<TreeNode<*>> = mutableListOf()
    var addressNodeid:Long? = null
    private var mId :String? = null
    private var fromAssets:Boolean? = null //我的资产过来的,要换个接口
    var isEdit = true //是否可编辑状态

    override fun onMVVMCreated() {
        mId = intent.extras?.getString(ID)
        fromAssets = intent.extras?.getBoolean(FROM_ASSETS)
        isEdit = intent.extras?.getBoolean(IS_EDIT,true)?:true
        initToolBar()
        initRv()
        initView()
        initVM()
        initData()
        vm.requestDetailInfo(mId,fromAssets)
    }

    private fun initData() {
        TreeViewUtils.loadData(2,userNodes)
//        TreeViewUtils.loadData(3,departmentNodes)
        TreeViewUtils.loadData(4,addressNodes)
    }

    private fun initVM() {
        vm.detail.observe(this){
            //只有待盘点可以编辑
            if(it.assetStatus?:-1 == 0 && isEdit){
                bindViews.tvHint.visibility = View.VISIBLE
                bindViews.rlModify.root.visibility = View.VISIBLE
                bindViews.llBottom.visibility = View.VISIBLE
            }
            bindViews.tvName.text = it.zichanmc
            val data = mutableListOf(
                Pair("资产编号", it.zichanbh),
                Pair("资产分类", it.zichanflmc),
                Pair("原值", it.price),
                Pair("取得日期", it.quderq),
                Pair("品牌", it.pinpai),
                Pair("型号", it.guigexh),
                Pair("存放地点", it.cunfangddid),
                Pair("使用人", it.shiyongrid),
                Pair("使用部门", it.shiyongglbmid),
                Pair("累计折旧", it.zhejiucount),
                Pair("数量", it.shuliang),
                Pair("备注", fromAssets!!.ternary(it.beizhu,it.assetRemark)),
            )
            mAdapter.setNewData(data)
        }
        vm.updateResult.observe(this){
            setResult(Activity.RESULT_OK)
            finish()
        }
    }

    private fun initView() {
        bindViews.rlModify.tvType.visibility = View.GONE
        bindViews.rlModify.etType.visibility = View.GONE
        bindViews.rlModify.ivTypeArraw.visibility = View.GONE
        bindViews.rlModify.etUser.setOnClickListener {
            showTreeViewPop(2,userNodes)
        }
        //部门由人员联动
        bindViews.rlModify.etDepartment.setOnClickListener {
//            showTreeViewPop(3,departmentNodes)
        }
        bindViews.rlModify.etDepartment.hint = ""
        bindViews.rlModify.ivDepartmentArraw.visibility = View.GONE
        bindViews.rlModify.etAddress.setOnClickListener {
            showTreeViewPop(4,addressNodes)
        }
        bindViews.tvLeft.setOnClickListener {
            vm.updateDetail(mId,2,
                bindViews.rlModify.etRemark.text?.toString(),
                bindViews.rlModify.etZc.text?.toString(),
                bindViews.rlModify.etBrand.text?.toString(),
                bindViews.rlModify.etModel.text?.toString(),
//                userNodeid?.toString(),
//                departmentNodeid?.toString(),
//                addressNodeid?.toString(),
                bindViews.rlModify.etUser.text?.toString(),
                bindViews.rlModify.etDepartment.text?.toString(),
                bindViews.rlModify.etAddress.text?.toString(),
                bindViews.rlModify.etShuliang.text?.toString(),
                )
        }
        bindViews.tvRight.setOnClickListener {
            vm.updateDetail(mId,1,
                bindViews.rlModify.etRemark.text?.toString(),
                bindViews.rlModify.etZc.text?.toString(),
                bindViews.rlModify.etBrand.text?.toString(),
                bindViews.rlModify.etModel.text?.toString(),
//                userNodeid?.toString(),
//                departmentNodeid?.toString(),
//                addressNodeid?.toString(),
                bindViews.rlModify.etUser.text?.toString(),
                bindViews.rlModify.etDepartment.text?.toString(),
                bindViews.rlModify.etAddress.text?.toString(),
                bindViews.rlModify.etShuliang.text?.toString(),
            )
        }
        bindViews.ivArraw.setOnClickListener {
            if(bindViews.rv.visibility == View.VISIBLE){
                bindViews.rv.visibility = View.GONE
                bindViews.ivArraw.setImageResource(R.mipmap.ic_arraw_down)
            }else{
                bindViews.rv.visibility = View.VISIBLE
                bindViews.ivArraw.setImageResource(R.mipmap.ic_arraw_up)
            }
        }
        bindViews.tvHint.visibility = View.GONE
        bindViews.rlModify.root.visibility = View.GONE
        bindViews.llBottom.visibility = View.GONE
    }



    private fun initRv() {
        bindViews.rv.layoutManager = LinearLayoutManager(this)
        mAdapter = bindViews.rv.bindAdapter { item, position ->
            tvKey.text = item.first
            tvValue.text = item.second
        }
    }

    private fun initToolBar() {
        toolBar = setToolBar(CommonToolBar::class.java)
        toolBar.setBackAction { finish() }
        toolBar.setTitle(fromAssets!!.ternary("资产详情","盘点详情"))
    }

    private fun showTreeViewPop(type:Int,nodes:MutableList<TreeNode<*>>) {
        if(nodes.isEmpty()){
            ToastUtils.show("接口数据为空,请返回首页重新获取")
            return
        }
        val treeViewPopupView = TreeViewPopupView(this)
        treeViewPopupView.setData(nodes) {
            //组织人员树的组织节点不做处理
            if(type == 2 && it.content is DepartmentModel && !(it.content as DepartmentModel).isUser){
                return@setData
            }
            treeViewPopupView.dismiss()
            val treeNode = it as TreeNode<NodeModel>
            when(type){
                2 ->  {
                    bindViews.rlModify.etUser.setText(treeNode.content.name)
                    userNodeid = treeNode.content.getmId()
                    bindViews.rlModify.etDepartment.setText((it.content as DepartmentModel).departmentName)
                }
//                3 ->  {
//                    bindViews.rlModify.etDepartment.setText(treeNode.content.name)
//                    departmentNodeid = treeNode.content.getmId()
//                }
                4 ->  {
                    bindViews.rlModify.etAddress.setText(treeNode.content.name)
                    addressNodeid = treeNode.content.getmId()
                }
            }

        }
        XPopup.Builder(this)
            .asCustom(treeViewPopupView)
            .show()
    }


}